import { Song } from '../../player/init-songs'
import { _getMuiscUrl } from '../../network/detail'

export const playMusic = {
  methods: {
    playMusic(index = 0) {
      const id = (this.$refs.songList && this.$refs.songList.id) || this.id || 'no-id'
      let musicList = []
      if (this.musicList.length >= 200) {
        return (musicList = this.musicList.slice(0, 199))
      } else {
        musicList = this.musicList
      }

      let url = null
      const playList = []
      for (let i = 0; i < musicList.length; i++) {
        _getMuiscUrl(musicList[i].id).then((res) => {
          url = res.data.data[0].url
          const song = new Song(i, musicList[i], url, musicList[i].id)
          playList.push(song)
          if (i === musicList.length - 1) {
            this.$bus.$emit('playMusic', playList, index, musicList, id)
          }
        })
      }
    }
  }
}
